<template>
	<view class="full-screen">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true"><block slot="backText">项目设置</block></cu-custom>
		<view class="cu-form-group" >
			<view class="cu-form-group">
				<input type="text" v-model="project.projectName" style="size: 20px;"/>
			</view>
		</view>
		<view class="cu-form-group" :class="menuArrow ? 'arrow' : ''" @click="selectStaff" style="display: flex; justify-content: space-between;">
		  <view class="cu-form-group" style="display: flex; align-items: center;">
		    <text class="">项目参与人</text>
		  </view>
		  <view class="action" style="display: flex; align-items: center;">
		    <view class="cu-avatar-group" v-for="item in ids" :key="item.id" style="padding: 0;">
		      <view class="cu-avatar round sm" :style="{ 'background-image': 'url(' + item.avatar + ')' }" style="margin-left: -10px;"></view>
		    </view>
		    <text class="text-grey text-sm">{{ ids.length }} 人</text>
		  </view>
		</view>
		<view class="" style="display: flex;margin-top: 400px;">
		  <button @click="delProgress" style="width: 300px;color: crimson;">删除项目</button>
		  <button @click="saveProgress" style="width: 300px;">保存</button>
		</view>
	</view>
</template>

<script>
	import request from '../../common/request';
	import common from 'common/common.js';
	export default {
		data() {
			return {
				project: {},
				menuArrow:'',
				ids:[],
				projectId:'',
				
			}
		},
		onLoad(options) {
		    // 从options中获取传递过来的字符串
		    this.projectId = options.projectId
		    this.getProjectById();
		},
		onShow() {
			
			var ids = uni.getStorageSync("ids");
			if(ids[0]){
				this.project.ids = this.ids.map(item => item.id);
			}
			this.staff = uni.getStorageSync("staff");
			
			// this.ids = uni.getStorageSync("ids");
			// if(this.ids){
			// 	this.project.ids = this.ids.map(item => item.id);
			// }
			// uni.removeStorageSync("ids");
		},
		methods: {
			// 获取当前项目详细
			getProjectById(){
				var _this = this;
				request({
					url: common.api_base_url+'/okrproject/' + _this.projectId,
					data:{
					},
					header:{
					},
					method: 'GET',
					success: res => {
						_this.project = res.data;
						_this.ids = _this.project.staffList;
						_this.project.ids = _this.project.staffList;
					},
					fail: function(e) {
						uni.showToast({
							title:"唉哟，程序错误了["+JSON.stringify(e)+"]",
							icon:"loading"
						})
					},
				});
			},
			// 选择参与人员
			selectStaff(){
				// 选中了之后，回显
				uni.navigateTo({
					url:'/pages/addTask/selectStaff?ids='+ encodeURIComponent(JSON.stringify(this.ids)),
				})
			},
			__e(){},
			// 修改项目
			saveProgress(){
				// 参与人员id
				if(this.ids){
					// 将自己添加到taskAndStaff表
					this.project.ids = this.ids.map(item => item.id);
				}
				var _this = this;
				request({
					url: common.api_base_url+'/okrproject',
					data:{
						"project": _this.project
					},
					header:{
					},
					method: 'PUT',
					success: res => {
						if(res.data.success){
							uni.showToast({
								title:"修改成功",
								duration: 1000
							})
						} else {
							uni.showToast({
								title: res.data.message,
								duration: 2000,
								icon:"error"
							})
						}
					},
					fail: function(e) {
						uni.showToast({
							title:"唉哟，程序错误了["+JSON.stringify(e)+"]",
							icon:"loading"
						})
					},
				});
			},
			// 删除项目
			delProgress(){
				let _this = this;
				uni.showModal({
				    title: '删除任务',
				    content: '您确定要删除这条任务吗？',
				    success: function (res) {
				        if (res.confirm) {
							request({
								url: common.api_base_url+'/okrproject/' + _this.project.id,
								data:{
								},
								header:{
								},
								method: 'DELETE',
								success: res => {
									if(res.data.success){
										uni.navigateBack({
										    delta: 1,
										    success: function () {
										        const pages = getCurrentPages(); //获取当前页面栈
										        const prevPage = pages[pages.length - 1]; //获取上一个页面实例对象
										        prevPage.onLoad(); //调用上一个页面的onLoad方法
										    }
										});
									} else {
										uni.showToast({
											title: res.data.message,
											duration: 2000,
											icon:"error"
										})
									}
								},
								fail: function(e) {
									uni.showToast({
										title:"唉哟，程序错误了["+JSON.stringify(e)+"]",
										icon:"loading"
									})
								},
							});
				        } else if (res.cancel) {
							res.cancel = true;
				        }
				    }
				});
			},
		}
	}
</script>

<style>
.full-screen {
  height: 100vh; /* 100% 屏幕高度 */
}

</style>
